package fi.foyt.cs.api.json;

import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import fi.foyt.cs.controller.ItemController;
import fi.foyt.cs.persistence.domainmodel.items.Item;
import fi.foyt.cs.persistence.domainmodel.task.TaskRewardItem;

public class TaskRewardItemRenderer implements EntityRenderer<TaskRewardItem> {

  @Override
  public JSONObject getAsJson(Locale locale, TaskRewardItem taskRewardItem) {
    JSONObject jsonObject = new JSONObject();

    ItemController itemController = new ItemController();
    
    try {
      Item item = itemController.findItemById(taskRewardItem.getItemId());
      
      jsonObject.put("id", taskRewardItem.getId().getId());
      jsonObject.put("count", taskRewardItem.getCount());
      jsonObject.put("item", EntityRendererVault.getInstance().renderEntity(locale, item));
      
    } catch (JSONException e1) {
      throw new RenderingException(e1);
    }

    return jsonObject;
  }

}
